'''
Created on Sep 22, 2011

@author: oabalbin
'''
def sequenced_regions_2paper(file,ofile):
    '''
    '''
    ofile=open(ofile,'w')
    ifile = open(file)
    ifile.next()
    header=True
    
    for i, l in enumerate(ifile):
        if header:
            h=l.strip('\r\n').split('\r')
            print h
            header=False
            continue
        
#        if i < 20:
        f=l.strip('\r\n').split('\t')
        f=l.strip('\r\n').split('\t')
        if len(f) >1:
            #print f
            ccds, geneID,exon, loc=f[0],f[1],f[2],f[3]
            if ccds:
                cur_ccds,cur_geneID=ccds,geneID
            if loc!='':
                chr, loc = loc.split(':')[0],loc.split(':')[1]
                start, end = loc.split('-')[0],loc.split('-')[1]
                ol = [chr, start, end, cur_ccds+'|'+cur_geneID+'|'+exon+'|'+str((int(end)-int(start))),'\n']
                ofile.write(",".join(ol).replace(',','\t'))
            
            else:
                continue
                #print "empty",f

def sequenced_regions_1paper(file):
    '''
    '''
    ifile = open(file)
    ifile.next()
    header=ifile.next()
    print header
    header=True
    for i, l in enumerate(ifile):
        if header:
            h=l.strip('\r\n').split('\r')
            #print h
            header=False
            
        if i < 10:
            f=l.strip('\r\n').split('\t')
            ccds, geneID,exon, loc=f[0],f[1],f[2],f[3]
            chr, start, end = loc.split(':')[0],loc.split(':')[1].split('-')[0],loc.split(':')[1].split('-')[1]
            
            print chr, start, end, ccds, geneID,exon





file='/exds/users/oabalbin/projects/exomes/HCCs_sequenced_regions/Table_S1_Primers_used_for_PCR_amplification_and_sequencing.txt'
file2='/exds/users/oabalbin/projects/exomes/HCCs_sequenced_regions/1145720table_S1.txt'
ofile1='/exds/users/oabalbin/projects/exomes/HCCs_sequenced_regions/HCCs_sequenced_regions1.hg17.bed'
ofile2='/exds/users/oabalbin/projects/exomes/HCCs_sequenced_regions/HCCs_sequenced_regions2.hg17.bed'
sequenced_regions_2paper(file,ofile1)
